import { onMounted, onUnmounted } from "vue"
export function use快捷键(cfg: 快捷键cfg = {}) {
    onMounted(() => {
        // console.log('绑定快捷键ing: ');
        document.addEventListener("keydown", handleKeyDown)
    })

    onUnmounted(() => {
        // console.log('注销 绑定快捷键ing: ');
        document.removeEventListener("keydown", handleKeyDown)
    })

    function handleKeyDown(event: KeyboardEvent) {
        // console.log(`handleKeyDown ing👇`)
        // console.log(`event 👉`, event)
        if (event.ctrlKey && event.key === "s") {
            // console.log('Ctrl + S 被按下了！');
            if (cfg.ctrl_s) {
                cfg.ctrl_s(event)
            }
        }
        // if (event.esc) {
        if (event.key === "Escape") {
            if (cfg.esc) {
                cfg.esc(event)
            }
        }
    }

    return { handleKeyDown }
}
